'use strict'

const path = require('path')
const { BrowserWindow } = require('electron')
const log4js = require('log4js')

const AppConfig = require('../configuration')

let Common = (AppConfig.readSettings('language') === 'zh_CN') ? require('../common_cn') : require('../common')

class SplashWindow {
  constructor() {
    this.logger = log4js.getLogger("SPLASH")
    this.logger.level = AppConfig.readSettings("logger-level")
    this.splashWindow = new BrowserWindow({
      width: Common.WINDOW_SIZE_LOADING.width,
      height: Common.WINDOW_SIZE_LOADING.height,
      title: Common.ELECTRONJS_WECHAT,
      type: "splash",
      resizable: false,
      center: true,
      show: true,
      frame: false,
      autoHideMenuBar: true,
      alwaysOnTop: true,
      icon: '../renderer/assets/icon.png',
      titleBarStyle: 'hidden',
    })

    this.splashWindow.loadURL(`file://${path.join(__dirname, '../renderer/views/splash.html')}`)

    this.splashWindow.on('closed', (event, isAlwaysOnTop) => {
      this.splashWindow = null
    })
  }

  show() {
    this.logger.debug("splash window show")
    this.splashWindow.show()
    this.isShown = true
  }

  hide() {
    this.logger.debug("splash window hide")
    this.splashWindow.hide()
    this.isShown = false
  }

  close() {
    this.logger.debug("splash window close")
    this.isShown = false
    this.splashWindow.close()
  }
}

module.exports = SplashWindow
